package org.epanetgrid.otimizacao.ag;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jheuristics.Individual;
import org.jheuristics.ga.BulkFitnessEvaluator;
import org.jheuristics.ga.GAConfig;
import org.jheuristics.ga.GAStatus;
import org.jheuristics.ga.Population;
import org.jheuristics.util.BitString;

/**
 * Realiza a avalia��o dos indiv�duos a partir de um mapa de simula��es anteriores de outros indiv�duos. Caso
 * um indiv�duo esteja para ser avaliado e "este" n�o tenha sido simulado anteriormente, um erro ocorrera. 
 * Todo espa�o de busca dos indiv�duos deve ser passado no construtor.
 * @author Marcell Manfrin, marcell@ourgrid.org, Oct 6, 2005
 */
public class SimpleGASimulatedBulkFitnessEvaluator implements BulkFitnessEvaluator {
	private Map<List<BitString>, Individual> map;

	/**
	 *
	 * @param individuals Lista de indiv�duos que j� foram simulados.
	 */
	@SuppressWarnings("unchecked")
	public SimpleGASimulatedBulkFitnessEvaluator(List<Individual> individuals) {
		map = new HashMap<List<BitString>, Individual>();
		for (Iterator it = individuals.iterator(); it.hasNext(); ) {
			Individual individual = (Individual) it.next();
			map.put((List<BitString>) individual.getGens(), individual);
		}
	}

	
	/* (non-Javadoc)
	 * @see org.jheuristics.ga.BulkFitnessEvaluator#evaluateFitness(org.jheuristics.ga.Population, org.jheuristics.ga.GAStatus, org.jheuristics.ga.GAConfig)
	 */
	public void evaluateFitness(Population population, GAStatus status, GAConfig config) {
		for (Iterator it = population.iterator(); it.hasNext(); ) {
			Individual individual = (Individual) it.next();
			Individual simulated = (Individual) map.get(individual.getGens());
			individual.setDataApplication(simulated.getDataApplication());
			individual.setFitness(simulated.getFitness());
		}
	}

}
